#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <vector>
#include <unordered_map>

const std::string path = "./word/E_C.txt";

class Dictionary
{
public:
    Dictionary()
    {
        Set();
    }

    void Updata()
    {
        lines.clear();
        dict.clear();
        Set();
    }

    void Set()
    {
        std::ifstream file(path);
        if (!file.is_open())
        {
            std::cerr << "打开文件失败" << std::endl;
            exit(1);
        }

        std::string buffer;
        while (std::getline(file, buffer))
        {
            lines.push_back(buffer);

            //读取空行
            std::getline(file, buffer);
        }
        
        //切分
        for (auto& line : lines)
        {
            size_t left = line.find(" ");
            //真正的单词是从left + 1开始找的
            size_t right = line.find(" ", left + 1);
            dict.insert(std::make_pair(line.substr(left + 1, right - left - 1), line.substr(right + 1)));
        }
    }

    std::string Find(std::string word)
    {
        auto it = dict.find(word);
        if (it == dict.end())
        {
            return "Not found";
        }
        else 
        {
            return it->second;
        }
    }

private:
    std::vector<std::string> lines;
    std::unordered_map<std::string, std::string> dict;
};